package com.wanmei.controller.product;

import com.wanmei.exception.WanmeiException;
import com.wanmei.pojo.Attribute;
import com.wanmei.pojo.Product;
import com.wanmei.pojo.ProductStoreNum;
import com.wanmei.pojo.Productpic;
import com.wanmei.service.*;
import com.wanmei.service.Impl.ProductServiceImpl;
import com.wanmei.service.Impl.ProductpicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductSellController {
    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private ProductpicServiceImpl productpicService;
    @Autowired
    private AttributeService attributeService;
    @Autowired
    private TransportService transportService;
    @Autowired
    private ProductKuidService productKuidService;


    /**
     * 点击产品信息的时候，跳出产品的界面
     * @param pid
     * @return
     * @throws WanmeiException
     */
    @RequestMapping("/showProductInfo.do")
    public ModelAndView showProductInfo(@RequestParam("pid") int pid, HttpServletRequest req) throws WanmeiException {
        ModelAndView mv = new ModelAndView();
        //产品信息
        Product product = productService.selectByPrimaryKey(pid);
        mv.addObject("product", product);
        //默认配送信息
//        List<String> fromAddressName = transportService.getDefaulTransport(pid);
//        mv.addObject("fromAddressName",fromAddressName);
        //型号
        Attribute attribute = attributeService.getAttributeAll(pid);
        System.out.println("attribute"+attribute);
        mv.addObject("attribute",attribute);

        //库存
        List<ProductStoreNum> listProductNum = productKuidService.getProductStoreNum(pid);
        mv.addObject("listProductNum",listProductNum);

        for (ProductStoreNum p:listProductNum){
            System.out.println(p);
        }
        mv.setViewName("product_sell/sell_productdetail");
        return mv;
    }

    /**
     * 从当前型号或者款式和产品ID拿到库存
     * @param pid
     * @param vid
     * @return
     */
    @RequestMapping("/getStoreNum.json")
    @ResponseBody
    public Integer getStoreNum(@RequestParam("pid")int pid,@RequestParam("vid") int vid){
        return productKuidService.getStoreNumBySize(pid, vid);
    }

    /**
     * 通过产品编号和 送货地和收货地来计算运费
     *
     * @param pid
     * @param fromAddress
     * @param toAddress
     * @return
     */
    @RequestMapping("/getTransMoney.json")
    @ResponseBody
    public Double getTransMoney(@RequestParam("pid") int pid,
                                @RequestParam("fromAddress") String fromAddress,
                                @RequestParam("toAddress") String toAddress) {
        return transportService.getTransportMoney(pid, fromAddress, toAddress);
    }

}
